﻿var delay = 0;
var max = 0;
var min = 1000000;
var pingFlag = 0;

$("#spPing2").hide();
$("#btnPing").click(function () {
    if (pingFlag == 0) {
        pingFlag = 1;

        delay = 0;
        min = 1000000;
        max = 0;

        $(this).attr("disabled", true);
        $("#spPing").html("0");

        for (var i = 0; i < 10; i++) {
            setTimeout("ping(" + i + ")", 800 * (i + 1));
        }
    } else if (pingFlag == 1) {
        pingFlag = 2;
        $(this).html("重新测试");
        $("#spPing1").hide();
        $("#spPing2").show();

        $("#spAvrPing").html((delay / 10).toFixed(0));
        $("#spShake").html(max - min);
        $("#spLost").html("0");
    }
    else if (pingFlag == 2) {
        pingFlag = 0;
        $(this).html("开始测试");
        $("#spPing1").show();
        $("#spPing2").hide();

        $("#spPing").html("0");
    }
    return false;
});



function ping(i) {
    $.ajax({ url: "http://www.baidu.com" + '/' + Math.random() + '.html',  //设置一个空的ajax请求
        type: 'GET',
        dataType: 'html',
        timeout: 2000,
        beforeSend: function () {
            requestTime = new Date().getTime();
        },
        complete: function () {
            responseTime = new Date().getTime();
            var d = Math.abs(requestTime - responseTime);
            delay += d;
            min = min > d ? d : min;
            max = max > d ? max : d;

          
            
            $("#spPing").html(d);
            if (i == 9) {
            	
            
            	
                $("#btnPing").attr("disabled", false);
                $("#btnPing").click();
            }
        }
    });
}